package MyServlet;

import jakarta.servlet.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebFilter(filterName = "CharactorFilter2",urlPatterns = "/*",
            initParams = { @WebInitParam(name = "encodong", value = "UTF-8") })
public class CharactorFilter2 implements Filter {
    String encoding = null;

    public void init(FilterConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding");
    }

    public void destroy() {
        encoding = null;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (encoding != null){
            request.setCharacterEncoding(encoding);
            response.setContentType("text/html; charset=" + encoding);
        }
        chain.doFilter(request, response);
    }
}
